/**
 * router/index.ts
 *
 * Automatic routes for `./src/pages/*.vue`
 */

// Composables
import { createRouter, createWebHashHistory } from 'vue-router/auto'
import { routes } from 'vue-router/auto-routes'


const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      // component: () => import('@/layouts/default.vue'),
      children: [
        {
          path: '/',
          redirect: '/home'
        },
        {
          path: '/login',
          name: 'admin-login',
          component: () => import('@/views/admin/login.vue'), // 默认子页面
        },
        {
            name: 'Main',
            meta: '框架',
            path: '/main',
            component: () => import('@/views/admin/main'),
            children: [
                {
                    name: 'Home',
                    meta: '首页',
                    path: '/home',
                    component: () => import('@/views/admin/home')
                },
                // 将test.vue作为main的子路由
                {
                    name: 'Test',
                    meta: '测试页面',
                    path: '/test',
                    component: () => import('@/views/test.vue')
                },
            ]
        },
      ],
    },
  ],
})

// Workaround for https://github.com/vitejs/vite/issues/11804
router.onError((err, to) => {
  if (err?.message?.includes?.('Failed to fetch dynamically imported module')) {
    if (localStorage.getItem('vuetify:dynamic-reload')) {
      console.error('Dynamic import error, reloading page did not fix it', err)
    } else {
      console.log('Reloading page to fix dynamic import error')
      localStorage.setItem('vuetify:dynamic-reload', 'true')
      location.assign(to.fullPath)
    }
  } else {
    console.error(err)
  }
})

router.isReady().then(() => {
  localStorage.removeItem('vuetify:dynamic-reload')
})

export default router
